package com.cqjtu.eecs.utils;

public class Result<T> {
    private Integer status; // 本次请求的结果状态
    private String msg; // 对状态的描述
    private T data; // 本次请求返回的数据

    // 成功，无返回数据
    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    // 成功，有返回数据
    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }
    //  成功，指定code和数据
    public static Result success(ResultCode resultCode, Object data){
        Result result = new Result();
        result.setResultCode(resultCode);
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    // 失败，不指定原因
    public static Result error() {
        Result result = new Result();
        result.setResultCode(ResultCode.ERROR);
        return result;
    }
    // 失败，指定status,msg
    public static Result error(Integer status, String msg) {
        Result result = new Result();
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    // 失败，指定失败结果枚举
    public static Result error(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }



    private void setResultCode(ResultCode resultCode) {
        this.status = resultCode.code();
        this.msg = resultCode.msg();
    }
    public Result() {
    }

    public Result(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
